package com.yanqu.road.dao.impl.activity.feast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.feast.data.CrossFeastPvpNoticeData;
import com.yanqu.road.entity.activity.feast.data.CrossFeastPvpNoticeUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

public class CrossFeastPvpNoticeDataDaoImpl extends TempDataObjectDao<CrossFeastPvpNoticeData> {

    public static String getCrossFeastPvpNoticeDataSql = "select * from `t_c_feast_pvp_notice_data` where `activityId`=? and `round`=? order by `noticeTime` desc limit ?";


    /**
     * 获取全服公告
     */
    public LinkedList<CrossFeastPvpNoticeData> getCrossFeastPvpNoticeDataList(int activityId, int round, int campId, int limit) {
        LinkedList<CrossFeastPvpNoticeData> list = new LinkedList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossFeastPvpNoticeDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, round);
            call.setInt(i++, limit);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossFeastPvpNoticeData parse = parse(rs);
                list.addLast(parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String insertCrossFeastPvpNoticeDataSql = "insert into `t_c_feast_pvp_notice_data`(activityId,round,userId,user1,user2,noticeType,content,noticeTime,isRoll) values (?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossFeastPvpNoticeData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUser1())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUser2())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getNoticeType()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getContent()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getNoticeTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getIsRoll()));
        if (getDb().executeNoneQuery(insertCrossFeastPvpNoticeDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected boolean updateData(CrossFeastPvpNoticeData data) {
        return false;
    }

    @Override
    protected CrossFeastPvpNoticeData parseData(ResultSet rs) throws SQLException {
        CrossFeastPvpNoticeData data = new CrossFeastPvpNoticeData();
        data.setActivityId(rs.getInt("activityId"));
        data.setRound(rs.getInt("round"));
        data.setUserId(rs.getLong("userId"));
        data.setUser1(JSON.parseObject(rs.getString("user1"), new TypeReference<CrossFeastPvpNoticeUserData>() {
        }));
        data.setUser2(JSON.parseObject(rs.getString("user2"), new TypeReference<CrossFeastPvpNoticeUserData>() {
        }));
        data.setNoticeType(rs.getInt("noticeType"));
        data.setContent(rs.getString("content"));
        data.setNoticeTime(rs.getLong("noticeTime"));
        data.setIsRoll(rs.getInt("isRoll"));
        return data;
    }
}
